<?php

namespace App\Admin\Actions\ShopProduct;

use App\Library\Logic\ActivityZoneLogic;
use App\Library\Logic\OperateLogLogic;
use App\Library\Models\AdminShopProductModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class ShopProductActiveAction extends RowAction
{
    public $name = '活动';

    /**
     * handle
     * @param AdminShopProductModel $model
     * @param Request $request
     * @return Response
     */
    public function handle(AdminShopProductModel $model, Request $request): Response
    {
        $model->product_activity = $request->product_activity_type > 0 ? 1 : 2;
        $model->product_activity_type = $request->product_activity_type;
        $model->save();
        OperateLogLogic::add(Admin::user()->user_id, 'admin.product.activityAction');
        return $this->response()->success('操作成功')->refresh();
    }

    /**
     * handle
     * @param AdminShopProductModel $model
     * @return void
     */
    public function form(AdminShopProductModel $model): void
    {
        $this->select('product_activity_type', __('shop_product.product_activity_type'))
            ->default($model->product_activity_type)
            ->options(ActivityZoneLogic::query()->pluck('title', 'id')->toArray());
    }
}
